package com.ews.onlineexchange.service.observer;

import java.util.HashMap;

import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import javax.enterprise.event.Observes;
import javax.enterprise.event.TransactionPhase;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import javax.transaction.Transactional.TxType;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;

import com.ews.onlineexchange.model.Bulletin;
import com.ews.onlineexchange.model.TradeStatus;
import com.ews.onlineexchange.service.BulletinService;
import com.ews.onlineexchange.service.qualifier.BestBulletinProcessEvent;

@Stateless
public class StartBestChooseEvaluationProcessObserver {
	
	@Inject
	BulletinService bulletinService;
	
	@Inject
	EntityManager entityManager;
	
	@Inject
	RuntimeService				runtimeService;
	
	@Asynchronous
	@Transactional( value = TxType.NOT_SUPPORTED )
	public void listenBulletinClinch( @Observes @BestBulletinProcessEvent Bulletin bulletin ) {
//		bulletin = entityManager.find(Bulletin.class, bulletin.getId());
//		System.out.println("状态"+bulletin.getTradeStatus());
//		if ( bulletin.getTradeStatus().equals( TradeStatus.Waiting ) ) {
//			HashMap<String, Object> variables = new HashMap<String, Object>();
//			variables.put( "buyer", bulletin.getBuyer().getUsername() );
//			variables.put( "bulletinId", bulletin.getId() );
//			variables.put( "projectName", bulletin.getProjectName() );
//			variables.put( "chooseTime", bulletin.getChooseTime() );
//			
//			ProcessInstance process = runtimeService.startProcessInstanceByKey( "bestChooseAudit", bulletin.getId(), variables );
//		}
	}
	
}
